package com.voole.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.voole.entity.SysAction;
import com.voole.entity.SysUser;
import com.voole.service.IMenuService;
import com.voole.service.ISecurityService;
import com.voole.service.ISysUserService;

@Controller
public class SysUserController extends BaseController {

	private static Logger logger = Logger.getLogger(SysUserController.class);
	
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISecurityService securityService;
	
	@RequestMapping("/sysuser/sysuser.do")
    public ModelAndView sysuser(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView next = new ModelAndView("sys_user");
		try {
			String pageUserSeeButton= securityService.getSecurityButtonForPage(request);
			next.addObject("SECURITYBUTTONFORPAGEBYUSERROLE", pageUserSeeButton);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return next;
    }
	
	@RequestMapping("/sysuser/findByPage.do")
	public void findByPage(HttpServletRequest request,HttpServletResponse response) {
		try {
		    String start = request.getParameter("start");//数据起始位置
		    String length = request.getParameter("length");//数据长度
		    
			Map<String, Object> ajaxMap = sysUserService.findByPage(start, length);
			this.ajaxComplete(JSONObject.fromObject(ajaxMap).toString(), response);
			
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
    }
	
	@RequestMapping("/sysuser/save.do")
	public void save(HttpServletRequest request,HttpServletResponse response) {
		try {
		    String account = request.getParameter("account");
		    String name = request.getParameter("name");
		    String password = request.getParameter("password");
		    String roleIds = request.getParameter("roles");
		    
			Map<String, Object> ajaxMap = sysUserService.addUserAndRole(account, name, password, roleIds);
			this.ajaxComplete(JSONObject.fromObject(ajaxMap).toString(), response);
			
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
    }
	
	@RequestMapping("/sysuser/getsysuser.do")
	public void getsysuser(HttpServletRequest request,HttpServletResponse response) {
		try {
		    String userId = request.getParameter("userId");
			this.ajaxComplete(sysUserService.getSysUser(userId), response);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
    }
	
}
